//
//  CinemaViewController.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/17.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "CinemaViewController.h"
#import "StarView.h"

@interface CinemaViewController ()
{
    BOOL flags[100];
}

@end

@implementation CinemaViewController





- (void)viewDidLoad {
    self.title = @"影院";
    [super viewDidLoad];
    [self loadData];
    [self _createTableView];
    // Do any additional setup after loading the view.
}



- (UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    CinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:cinemaCell forIndexPath:indexPath];
    NSString *name = _nameArray[indexPath.section];
    NSArray *array = [_cinemaData valueForKey:name];
    Cinemas *data = array[indexPath.row];
    NSLog(@"%@", data.lowPrice);
    cell.cinema = data;
    return cell;
    
    
}

- (void)loadData {
    _cinemaData = [[NSMutableDictionary alloc] init];
    _nameArray = [[NSMutableArray alloc] init];
    _nameData = [[NSMutableArray alloc] init];
    NSDictionary *dic = [DataService readJsonFlieWithNamed:listJson];
    NSArray *array = [dic valueForKey:@"districtList"];
    for (NSDictionary *temp in array) {
        NSString *name = [temp valueForKey:@"name"];
        NSString *id = [temp valueForKey:@"id"];
        NSDictionary *list = @{name: id};
        [_nameArray addObject:name];
        [_nameData addObject:list];
    }
    for (NSString *str in _nameArray) {
        NSMutableArray *temp = [[NSMutableArray alloc] init];
        [_cinemaData setObject:temp forKey:str];
    }
    NSDictionary *name = [DataService readJsonFlieWithNamed:cinemaJson];
    NSArray *array1 = [name valueForKey:@"cinemaList"];
    for (NSDictionary *dic1 in array1) {
        for (NSDictionary * dic in _nameData) {
            NSArray *u = [dic allKeys];
            NSString *str = u.firstObject;
            NSString *id2 = [dic valueForKey:str];
            NSString *id1 = [dic1 valueForKey:@"districtId"];
            NSMutableArray *kk1 = [_cinemaData objectForKey:str];
            if ([id1 isEqualToString: id2]) {
                Cinemas *cinema = [[Cinemas alloc] init];
                cinema.grade = [[dic1 valueForKey:@"grade"] doubleValue];
                cinema.address = [dic1 valueForKey:@"address"];
                cinema.lowPrice = [dic1 valueForKey:@"lowPrice"];
                cinema.name = [dic1 valueForKey:@"name"];
                [kk1 addObject: cinema];
            }
            
        }
    }
    
}

- (void)_createTableView {
    _table = [[UITableView alloc] initWithFrame:self.view.bounds];
    [_table registerNib:[UINib nibWithNibName:cinemaCell bundle:nil] forCellReuseIdentifier:cinemaCell];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
}

- (nullable UIView *)tableView:(nonnull UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kWidth, 60)];
    button.tag = section;
    [button addTarget:self action:@selector(displayOrNo:) forControlEvents:UIControlEventTouchUpInside];
    UIImage *img = [UIImage imageNamed:@"btnbg_blue"];
    img = [img stretchableImageWithLeftCapWidth:5 topCapHeight:5];
    [button setBackgroundImage:img forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    NSString *name = _nameArray[section];
    [button setTitle:name forState: UIControlStateNormal];
    return button;
}





- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (flags[section]) {
        NSString *name = _nameArray[section];
        NSArray *array = [_cinemaData valueForKey:name];
        return array.count;
    } else {
        return 0;
    }
}
- (CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 100;
}

- (NSInteger)numberOfSectionsInTableView:(nonnull UITableView *)tableView {
   return _nameArray.count;
   
}

- (CGFloat)tableView:(nonnull UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}


- (void)displayOrNo: (UIButton *)button {
    flags[button.tag] = !flags[button.tag];
    NSIndexSet *set = [NSIndexSet indexSetWithIndex:button.tag];
   [_table reloadSections:set withRowAnimation:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
